package org.jash.common.mvvm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observer
import io.reactivex.rxjava3.disposables.Disposable
import org.jash.common.logging

open class BaseViewModel:ViewModel() {
    val errorLiveData by lazy { MutableLiveData<Throwable>() }
    fun<D : Any> progress(observable: Observable<D>, success:(D) -> Unit) {
        val disposable = observable.subscribe({
            success(it)
        }, errorLiveData::postValue)
        addCloseable { disposable.takeIf { !it.isDisposed }?.dispose() }
    }
}